/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fhv.itb.ec.fruitshop;

import java.io.Serializable;
import java.util.List;
import java.util.Vector;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

/**
 *
 * @author Malcolm
 */
@Entity
public class FruitShopEntity implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @OneToMany
    private List<Fruit> fruits;

    public FruitShopEntity() {
    }

    public Long getId() {
        return id;
    }


    /**
     * Puts a (1) fruit back in the shop.
     * This will only be successful if the fruit belongs to this shop.
     *
     * @param f The fruit to put back.
     *
     * @return true, if the fruit was put back.
     */
    public boolean putFruit(Fruit f) {
        boolean result = false;
        
        if (fruits == null) {
            fruits = new Vector<Fruit>();
        }

        for (Fruit f2 : fruits) {

            if (f2.getId().equals(f.getId())) {
                f2.setNumber(f2.getNumber() + 1);
                result = true;
            }

        }

        fruits.add(f);

        return result;
    }

    /**
     * Gets a fruit from this shop.
     *
     * @param id The id of the requested fruit.
     *
     * @return The fruit, or null if it cannot be found.
     */
    public Fruit getFruit(Long id) {

        if (fruits != null) {

            for (Fruit f : fruits) {

                if (f.getId().equals(id)) {
                    return f;
                }

            }

        }

        return null;
    }

    /**
     *
     * @return A list of all the fruits in this shop.
     */
    public List<Fruit> getFruits() {
        return fruits;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof FruitShopEntity)) {
            return false;
        }
        FruitShopEntity other = (FruitShopEntity) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "fhv.itb.ec.fruitshop.FruitShopEntity[id=" + id + "]";
    }
}
